<template>
	<view class="page">
		<view v-if="scrollTop<=460" class="backBox" :style="{top:menuTop+'px'}">
			<view class="Vr" style="padding: 10rpx;border-radius: 100%;">
				<u-icon name='arrow-left' color='#fff' @click='$utils.back()' size='30rpx' />
			</view>
		</view>
		<u-navbar v-else title="工地详情" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<view style="flex: 1;">
			<z-paging :refresher-enabled='refresher_enabled' ref="paging" style="height: 100%;" :fixed="false"
				v-model="activities" @query="query" :default-page-no="1" :default-page-size="10" :auto="false"
				@scroll='scroll' :hide-empty-view='true'>
				<view class="background">
					<template v-if="!loading">
						<view class="masking">
							<swiper class="swiper" circular autoplay>
								<swiper-item @click="clickImage(index)" class="swiperItem"
									v-for="(item,index) in images" :key="index">
									<view class="masking" />
									<image mode="aspectFill" :src="item"></image>
								</swiper-item>
							</swiper>
						</view>
					</template>
					<view v-else class="masking" style="background:transparent;" />
					<view :style="{top:menuTop+'px',right:menuRight+20+'px',height:menuHeight+'px'}" class="VrBox">
						<view v-if="vr_electricity_url" class="Vr" @click='copy2'
							style="border-radius: 100%;padding: 10rpx;">
							<u-icon name='attach' color='#fff' />
						</view>
						<view v-if="Vrimg" class="Vr" @click='copy' style="border-radius: 100%;padding: 10rpx;">
							<u-icon name='home' color='#fff' />
						</view>
						<view v-if="devices.length" class="Vr" @click="liveShow=true"
							style="border-radius: 100%;padding: 10rpx;">
							<u-icon name='camera' color='#fff' />
						</view>
					</view>
					<view v-if='!loading' class="userBox" :style="{top:menuTop+80+'px'}">
						<u-avatar :src='user.avatar' size='100rpx' />
						<view style="width: 20rpx;"></view>
						<view style="flex: 1;">
							<u-text :lines='1' :text='detail.remark_name' color='#fff' />
							<view style="height: 20rpx;"></view>
							<view style="display: flex;">
								<view>
									<u--text lines='1' :iconStyle='{color:"#fff"}' prefixIcon='map' color='#fff'
										:text='detail.district' size='24rpx' />
								</view>
								<view style="color: #fff;">
									·
								</view>
								<view>
									<u--text lines='1' :iconStyle='{color:"#fff"}' color='#fff'
										:text='detail.style.title' size='24rpx' />
								</view>
							</view>
						</view>
					</view>

					<view v-if='!loading' class="stepsBox avatarGroup">
						<view @click.stop="Followed" class="Vr" style="margin-bottom: 0;border-radius:100rpx ;">
							<u-icon v-if="!isFollowed" color='#fff' labelColor='#fff' name="heart" size="24rpx"
								labelSize='24rpx' label='关注'></u-icon>
							<u-icon v-else color='#fff' labelColor='#fff' name="heart-fill" size="24rpx"
								labelSize='24rpx' label='取消关注'></u-icon>
						</view>
						<view @click.stop="Thumbsup" class="Vr"
							style="margin-left: 20rpx;margin-bottom: 0;border-radius:100rpx ;">
							<u-icon v-if="!isThumbsuped" color='#fff' labelColor='#fff' name="thumb-up" size="24rpx"
								labelSize='24rpx' label='点赞'></u-icon>
							<u-icon v-else color='#fff' labelColor='#fff' name="thumb-up-fill" size="24rpx"
								labelSize='24rpx' label='取消点赞'></u-icon>
						</view>
						<view style="display: flex;align-items: center;flex: 1; justify-content: center;">
							<template v-if='thumbsupers.length'>
								<u-avatar-group :urls="thumbsupers" size="35" keyName='avatar'
									gap="0.4"></u-avatar-group>
								<view>
									喜欢Ta家
								</view>
							</template>
						</view>
						<view style="display: flex;align-items: center;">
							装修<view class="Vr" style=" margin: 0 10rpx; border-radius:50rpx;">
								{{detail.decorate_days}}
							</view>天
						</view>
					</view>
				</view>
				<view class="contentBox">
					<infoComponent :loading='loading' :detail='detail' />

					<active @refresher_enabled_change='refresher_enabled_change' ref='active' :playIndex='playIndex'
						:loading='loading' :prorcessesId='prorcessesId' :activities='activities' :processes='processes'
						@prorcessesChange='prorcessesChange' />
				</view>

			</z-paging>
		</view>
		<root-portal>
			<liveComponent :devices='devices' :show='liveShow' @close='liveShow=false' />
		</root-portal>
	</view>
</template>

<script>
	import active from './components/active.vue'
	import liveComponent from './components/live.vue'
	import infoComponent from './components/info.vue'
	export default {
		components: {
			active,
			liveComponent,
			infoComponent
		},
		onShareAppMessage() {
			return {
				title: `川祁邀您查看工地监管-${new Date().toLocaleDateString()}`,
				path: "/customer/detail_c?id=" + this.decorate_id,
				imageUrl: this.backgroundImg
			}
		}, //2.分享到朋友圈
		onShareTimeline(res) {
			return {
				title: `川祁邀您查看工地监管-${new Date().toLocaleDateString()}`,
				path: "/customer/detail_c?id=" + this.decorate_id,
				imageUrl: this.backgroundImg
			}
		},
		data() {
			return {
				vr_electricity_url: null,
				images: [],
				menuHeight: 0,
				menuRight: 0,
				detail: {},
				loading: true,
				activities: [],
				decorate_id: 0,
				processes: [],
				playIndex: 0,
				menuTop: 0,
				scrollTop: 0,
				navbarTop: 0,
				Vrimg: null,
				backgroundImg: null,
				thumbsupers: [],
				user: {},
				refresher_enabled: true,
				isThumbsuped: false,
				liveShow: false,
				devices: [],
				isFollowed: false,
				prorcessesId: 0
			}
		},
		onLoad(op) {
			this.decorate_id = op.id
			this.getDateil(op.id)
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect() //可以获取小程序胶囊的位置。
			this.menuTop = menuButtonInfo.top
			this.menuRight = menuButtonInfo.width
			this.menuHeight = menuButtonInfo.height
			let res = uni.getSystemInfoSync()
			this.navbarTop = res.statusBarHeight + 44 //顶部状态栏+顶部导航，大部分机型默认44px
		},
		methods: {
			clickImage(index) {
				uni.setStorageSync('imageList',
					this.detail.images.map(item => {
						return {
							url: item.url,
						}
					})
				);
				this.$utils.navigateTo({
					url: '/image/imageShow?index=' + index
				})
			},
			prorcessesChange(e) {
				this.prorcessesId = e
				this.$refs.paging.reload()
			},
			Thumbsup() {
				if (uni.getStorageSync('token')) {
					if (this.isThumbsuped) {
						uni.$u.toast('取消点赞')
						this.thumbsupers = this.thumbsupers.filter(item => item.avatar != uni.getStorageSync('user')
							.avatar)
					} else {
						uni.$u.toast('点赞成功')
						this.thumbsupers.push({
							avatar: uni.getStorageSync('user').avatar
						})
					}
					this.isThumbsuped = !this.isThumbsuped

				}
				this.$api.Thumbsup.Thumbsup('decorate', this.decorate_id).then(res => {

				}).catch(err => {
					uni.$u.toast('未登录请先登录')
				})

			},
			Followed() {
				if (uni.getStorageSync('token')) {
					if (this.isFollowed) {
						uni.$u.toast('取消关注')
						this.$api.Followed.DelFollow({
							user_id: this.detail.customer_id
						}).then(res => {}).catch(err => {
							uni.$u.toast(err.message)
							this.isFollowed = !this.isFollowed
						})
					} else {
						uni.$u.toast('关注成功')
						this.$api.Followed.Follow({
							user_id: this.detail.customer_id
						}).then(res => {}).catch(err => {
							uni.$u.toast(err.message)
							this.isFollowed = !this.isFollowed
						})
					}
					this.isFollowed = !this.isFollowed
				} else {
					let pages = getCurrentPages(); // 获取栈实例
					const perpage = pages[pages.length - 1]
					let url = '/' + perpage.route + '?'
					for (let i in perpage.options) {
						url = url + i + '=' + perpage.options[i] + '&'
					}
					uni.setStorageSync('pageUrl', url);
					uni.reLaunch({
						url: '/login/index'
					})
					uni.$u.toast('未登录请先登录')
				}

			},
			refresher_enabled_change(e) {
				this.refresher_enabled = e
			},

			copy() {
				uni.setClipboardData({
					data: this.Vrimg, //要被复制的内容
					success: () => { //复制成功的回调函数
						uni.$u.toast('已复制到粘贴板,请在浏览器中打开')
					},
					fail: (err) => {}
				});
			},
			copy2() {
				uni.setClipboardData({
					data: this.vr_electricity_url, //要被复制的内容
					success: () => { //复制成功的回调函数
						uni.$u.toast('已复制到粘贴板,请在浏览器中打开')
					},
					fail: (err) => {}
				});
			},
			getDateil(id) {
				uni.hideLoading({
					title: '加载中...'
				})
				this.$api.Decorate.Detail_C(id).then(res => {
					this.detail = res.data.decorate
					this.backgroundImg = res.data.decorate.thumb
					this.vr_electricity_url = this.detail.vr_electricity_url
					this.Vrimg = res.data.decorate.vr_image_url
					this.thumbsupers = res.data.decorate.thumbsupers
					this.user = res.data.decorate.customer
					this.processes = res.data.decorate.processes
					this.isThumbsuped = res.data.isThumbsuped
					this.devices = this.detail.devices
					this.isFollowed = res.data.isFollowed
					this.images = res.data.decorate.images.map(item => item.url)
					if (this.processes.length == 0) {
						uni.$u.toast('房屋还在拍摄中！！')
						setTimeout(() => {
							this.$utils.back()
						}, 1000)
						return
					}
					uni.hideLoading()
					this.query(1, 10)
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			query(page, size) {
				let obj = {
					page,
					size,
					media_type: 'all',
					sort: -1,
				}
				if (this.prorcessesId) {
					obj.process_id = this.prorcessesId
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.Active.List_C(this.decorate_id, obj).then(res => {
					this.$refs.paging.complete(res.data.activities.data.map(item => {
						return {
							...item,
							user: res.data.user[item.user_id],
							customer: res.data.customer
						}
					}))
					uni.hideLoading()
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			scroll(e) {
				const top = e.detail.scrollTop
				this.scrollTop = top * 2
				let index = 0
				let domTop0 = 0
				const query = uni.createSelectorQuery().in(this.$refs.active);
				query.selectAll('.activeBox').boundingClientRect(data => {
					data.forEach((rect, domIndex) => {
						let domTop = rect.top + top
						if (domIndex == 0) {
							domTop0 = domTop
						}
						if (top > (domTop - domTop0)) {
							index = domIndex
						}
						if (domIndex == data.length - 1) {
							this.playIndex = index

						}
					});
				}).exec();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		background-color: #f5f5f5;
		display: flex;
		flex-direction: column;
	}

	.stepsBox {
		padding: 0rpx 20rpx;
		height: 100rpx;
		position: absolute;
		z-index: 1;
		display: flex;
		overflow: auto;
	}

	.fff {
		background-color: #fff;
		width: 100%;
		z-index: 111;
	}



	.contentBox {
		flex: 1;
		position: relative;
	}

	.background {
		height: 460rpx;
		width: 100%;
		position: relative;
	}

	.masking {
		background: linear-gradient(to top, rgba(0, 0, 0, 0.7), rgba(255, 255, 255, 0), rgba(255, 255, 255, 0));
		position: absolute;
		width: 100%;
		height: 460rpx;
		background-repeat: no-repeat;
		background-position: center;
		z-index: 2;
	}

	.backBox {
		position: fixed;
		left: 40rpx;
		z-index: 1111111111;
	}

	.Vr {
		background: rgba(0, 0, 0, 0.3);
		padding: 10rpx 20rpx;
		border-radius: 8rpx;
		color: #fff;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;

		>button {
			opacity: 0;
			position: absolute;
			z-index: 1;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
		}
	}

	.VrBox {
		display: flex;
		position: absolute;
		align-items: center;
		z-index: 111;

		>view {
			margin-right: 10rpx;
		}
	}

	.userBox {
		display: flex;
		position: absolute;
		width: 90vw;
		left: 40rpx;
		z-index: 111;
	}

	.avatarGroup {
		z-index: 111;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		justify-content: space-between;
		align-items: center;
		width: 100vw;
		box-sizing: border-box;
	}

	.thumbsupersBox {

		bottom: 0;
		height: auto;
		right: -20rpx;
	}

	.swiper,
	.swiperItem {
		width: 100%;
		height: 100%;

		image {

			width: 100%;
			height: 100%;
		}
	}
</style>